package org.cell.server.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class DBHelperImpl implements IDBHelper {

	// 后期配置到spring中
	public final String USERNAME = "root";
	public final String PASSWORD = "root";

	public final String MYSQL_URL = "jdbc:mysql://localhost:3306/cell";
	public final String CLASS_NAME = "com.mysql.jdbc.Driver";
	//

	private Connection con = null;

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#getConnection()
	 */
	@Override
	public Connection getConnection() {
		try {
			Class.forName(CLASS_NAME);
			con = DriverManager.getConnection(MYSQL_URL, USERNAME, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return con;
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#getStm(java.sql.Connection)
	 */
	@Override
	public Statement getStm(Connection con) {
		Statement stm = null;
		try {
			if (con != null) {
				stm = con.createStatement();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stm;
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#update(java.sql.PreparedStatement, java.lang.String, java.util.List)
	 */
	@Override
	public void update(PreparedStatement pstm, String sql,
			List<Parameter> params) {
		try {
			if (con != null) {
				pstm = con.prepareStatement(sql);
				for (Parameter param : params) {
					pstm.setObject(param.getIndex(), param.getValue(),
							param.getSqlType());
				}
				pstm.executeUpdate();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstm != null) {
					pstm.close();
					pstm = null;
				}
			} catch (Exception e2) {
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#find(java.sql.PreparedStatement, java.lang.String)
	 */
	@Override
	public ResultSet find(PreparedStatement pstm, String sql) {
		return find(pstm, sql, null);
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#find(java.sql.PreparedStatement, java.lang.String, java.util.List)
	 */
	@Override
	public ResultSet find(PreparedStatement pstm, String sql,
			List<Parameter> params) {
		ResultSet rs = null;
		try {
			pstm = con.prepareStatement(sql);
			if (params != null) {
				for (Parameter param : params) {
					pstm.setObject(param.getIndex(), param.getValue(),
							param.getSqlType());
				}
			}
			rs = pstm.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#getRs(java.sql.Statement, java.lang.String)
	 */
	@Override
	public ResultSet getRs(Statement stm, String sql) {
		ResultSet rs = null;
		try {
			if (stm != null) {
				rs = stm.executeQuery(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#update(java.sql.Statement, java.lang.String)
	 */
	@Override
	public int update(Statement stm, String sql) {
		int records = 0;
		try {
			if (stm != null) {
				records = stm.executeUpdate(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return records;
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#delete(java.sql.Statement, java.lang.String)
	 */
	@Override
	public boolean delete(Statement stm, String sql) {
		boolean flag = false;
		try {
			if (stm != null) {
				flag = stm.execute(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#getPrepareSql(java.lang.String, java.util.List)
	 */
	@Override
	public String getPrepareSql(String sql, List<Parameter> params) {
		// TEMP
		return null;
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#close(java.sql.Connection)
	 */
	@Override
	public void close(Connection con) {
		try {
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#close(java.sql.Statement)
	 */
	@Override
	public void close(Statement stm) {
		try {
			if (stm != null) {
				stm.close();
				stm = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#close(java.sql.ResultSet)
	 */
	@Override
	public void close(ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.cell.server.common.IDBHelper#close(java.sql.PreparedStatement)
	 */
	@Override
	public void close(PreparedStatement pstm) {
		try {
			if (pstm != null) {
				pstm.close();
				pstm = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
